package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口类")
public class CategoryController {
//    调用服务层的类
    @Autowired
    CategoryService categoryService;

    /**
     * 分页查询，显示数据的功能
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO dto){
        log.info("分页查询开始...");
        PageResult p = categoryService.pageQuery(dto);
        return Result.success(p);
    }

    /**
     * 添加分类的功能
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("添加分类")
    public  Result add(@RequestBody CategoryDTO dto){
        log.info("添加{}开始...",dto.getType() == 1? "菜品类":"套餐类");
        categoryService.add(dto);
        return Result.success();
    }

    /**
     * 删除分类的功能
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result delete(Long id){
        log.info("分类删除中...");
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 分类的修改
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("分类修改")
    public Result edi(@RequestBody CategoryDTO categoryDTO){
        log.info("{}分类修改修改中...",categoryDTO.getName());
        categoryService.edi(categoryDTO);
        return Result.success();
    }

    /**
     * 分类的启用禁用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("分类的启用禁用")
    public Result startOrBin(@PathVariable Integer status,Long id){
        log.info("分类的状态改为：{}，{}",status == 1 ? "启售": "停售",id);
        categoryService.startOrBin(status,id);
        return Result.success();
    }

    /**
     * 根据菜品分类进行查询
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("菜品分类查询")
    public Result<List<Category>> list(Integer type){
        log.info("{}的分类查找中...",type ==1 ? "菜品类":"套餐类");
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

}
